<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 *
 * The TYPOlight webCMS is an accessible web content management system that 
 * specializes in accessibility and generates W3C-compliant HTML code. It 
 * provides a wide range of functionality to develop professional websites 
 * including a built-in search engine, form generator, file and user manager, 
 * CSS engine, multi-language support and many more. For more information and 
 * additional TYPOlight applications like the TYPOlight MVC Framework please 
 * visit the project website http://www.typolight.org.
 *
 * PHP version 5
 * @copyright  Martin Komara 2007 
 * @author     Martin Komara 
 * @package    CatalogModule 
 * @license    GPL 
 * @filesource
 */


/**
 * Class ModuleCatalogToday 
 *
 * @copyright  Martin Komara 2007 
 * @author     Martin Komara 
 * @package    Catalog
 */
class ModuleCatalogToday extends Module
{
    /**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'mod_catalog_today';
	
	/**
	 * Display a wildcard in the back end
	 * @return string
	 */
	public function generate()
	{
		if (TL_MODE == 'BE')
		{
			$objTemplate = new BackendTemplate('be_wildcard');

			$objTemplate->wildcard = '### PRODUCT TODAY ###';
			$objTemplate->title = $this->name;
			$objTemplate->id = $this->id;
			$objTemplate->link = $this->alias;
			$objTemplate->href = 'typolight/main.php?do=Product catalog&amp;table=tl_catalog_items&amp;act=edit&amp;id=' . $this->id;

			return $objTemplate->parse();
		}
		
		// session catalog_discount_product
		if (!isset($_SESSION['TODAY']['catalog_discount_product']) && !$_SESSION['TODAY']['catalog_discount_product'])
			$_SESSION['TODAY']['catalog_discount_product'] = $this->catalog_discount_product;
		
		return parent::generate();
	}


	/**
	 * Generate module
	 */
	protected function compile()
	{
		$objRd 		= $this->getProductDetail();
		$colours	= $this->getColour();
		$tags		= $this->getTag();
		$sizes		= $this->getSize();
		
		// parse the product.
		$arrColourIDs = array_keys($objRd['colour']);
		$colourIDs = $arrColourIDs;
		$strColourIDs = implode(',', $arrColourIDs);
		$objRd['colour_ids'] = $strColourIDs;
		$this->Template->colourIDs = $arrColourIDs;
		if ($this->Input->get('id_colour'))
		{
			$this->Template->colour = $colours[$this->Input->get('id_colour')]['title'];
			$arrSizeIDs = array_keys($objRd['colour'][$this->Input->get('id_colour')]);
			$strSizeIDs = implode(',', $arrSizeIDs);
		}
		else
		{
			$this->Template->colour = $colours[$arrColourIDs[0]]['title'];
			$arrSizeIDs = array_keys($objRd['colour'][$arrColourIDs[0]]);
			$strSizeIDs = implode(',', $arrSizeIDs);
		}
		
		// stock.
		if ($this->Input->get('id_colour'))
		{
			if ($this->Input->get('id_size'))
			{
				foreach ($objRd['attribute'] as $arrAttributeItem)
				{
					if ($arrAttributeItem['colour'] == $this->Input->get('id_colour') && $arrAttributeItem['size'] == $this->Input->get('id_size'))
					{
						$currentStock = intval($arrAttributeItem['stock']);
						break;
					}
				}
			}
			else
			{
				/*foreach ($objRd['attribute'] as $arrAttributeItem)
				{
					if ($arrAttributeItem['colour'] == $_GET['id_colour'])
					{
						$currentStock = intval($arrAttributeItem['stock']);
						break;
					}
				}*/
				$currentStock = null;
			}				
		}
		else
		{
			if ($this->Input->get('id_size'))
			{
				foreach ($objRd['attribute'] as $arrAttributeItem)
				{
					if ($arrAttributeItem['size'] == $this->Input->get('id_size') && $colourIDs[0] == $arrAttributeItem['colour'])
					{
						$currentStock = intval($arrAttributeItem['stock']);
						break;
					}
				}
			}
			else
			{
				//$currentStock = intval($objRd['attribute'][0]['stock']);
				$currentStock = null;
			}
		}
		
		$objRd['size_ids'] = $strSizeIDs;
		$this->Template->size = ($this->Input->get('id_size')) ? $this->Input->get('id_size') : 0;
		$this->Template->sizeIDs = $arrSizeIDs;		
		$this->Template->products = $objRd;
		$this->Template->currentStock = $currentStock;
		
		// get Tags
		if($objRd['tags'])
		{
			$this->Template->tagIDs = explode(",", $objRd['tags']);
		}

		// get Colour
		//$this->Template->colourIDs = explode(",", $objRd['colour']);
		$this->Template->colours = $colours;
		
		// set Images
		$this->Template->colourPath = "daft_files/catalog/products/".$objRd['skuid'] . "/";
	
		$this->Template->colourPath .= $this->Template->colour . "/";	
		
		// get Size
		//$this->Template->sizeIDs = explode(",", $objRd['size']);
		$this->Template->sizes = $sizes;			
	}
	
	private function getColour()
	{
		$objRd = $this->Database->execute("SELECT * FROM `catalog_colour` Order by id asc");
		
		while ($objRd->next())
		{
			$dataArray[$objRd->id]['title'] = $objRd->title;
			$dataArray[$objRd->id]['img'] 	= $objRd->img;
		}
		return $dataArray;
	}
	
	private function getTag()
	{
		$objRd = $this->Database->execute("SELECT * FROM `catalog_tags` Order by id asc");
		
		while ($objRd->next())
		{
			$dataArray[$objRd->id]['tag'] = $objRd->tag;
		}
		return $dataArray;
	}
	
	private function getSize()
	{
		$objRd = $this->Database->execute("SELECT * FROM `catalog_size` Order by id asc");
		
		while ($objRd->next())
		{
			$dataArray[$objRd->id]['size'] = $objRd->size;
		}
		return $dataArray;
	}
	
	private function getProductDetail()
	{
		
		if (!$this->catalog_discount_product)
			return array();
		
		/*$sql   = 'SELECT product_catalog.*,tl_taxonomy.name as categoryName,catalog_wrapping.title as gwTitle,catalog_wrapping.img as gwImg,catalog_wrapping.price as gwPrice FROM `product_catalog`,`tl_taxonomy`,`catalog_wrapping` where product_catalog.id='.$this->catalog_discount_product.' and product_catalog.category=tl_taxonomy.id and product_catalog.wrapping=catalog_wrapping.id';*/
		$sql   = 'SELECT product_catalog.*,tl_taxonomy.name as categoryName,catalog_wrapping.title as gwTitle,catalog_wrapping.img as gwImg,catalog_wrapping.price as gwPrice,tl_catalog_attribute.id_catalog,tl_catalog_attribute.id_color,tl_catalog_attribute.id_size,tl_catalog_attribute.catalog_stock   
				  FROM `product_catalog` 
				  LEFT JOIN `tl_taxonomy` ON product_catalog.category=tl_taxonomy.id 
				  LEFT JOIN `catalog_wrapping` ON product_catalog.wrapping=catalog_wrapping.id 
				  LEFT JOIN `tl_catalog_attribute` ON `tl_catalog_attribute`.id_catalog=`product_catalog`.id 
				  WHERE product_catalog.id=' . $this->catalog_discount_product .
				  ' ORDER BY `tl_catalog_attribute`.id_color ASC, `tl_catalog_attribute`.id_size ASC';
		$objRd = $this->Database->execute($sql);
		
		if ($objRd->numRows < 1)
			return array();	
			
		$index = 0;
		while ($objRd->next())
		{
			if ($index == 0)
			{
				$rdArray['id'] 			= $objRd->id;
				$rdArray['pid'] 		= $objRd->pid;
				$rdArray['category'] 	= $objRd->category;
				$rdArray['categoryName']= $objRd->categoryName;
				$rdArray['name'] 		= $objRd->name;
				$rdArray['description'] = $objRd->description;
				$rdArray['price'] 		= $objRd->price;
				$rdArray['skuid'] 		= $objRd->skuid;
				//$rdArray['colour'] 		= $objRd->colour;
				//$rdArray['size'] 		= $objRd->size;
				$rdArray['related_skuid'] = $objRd->related_skuid;
				$rdArray['gwTitle'] 	= $objRd->gwTitle;
				$rdArray['gwImg'] 		= $objRd->gwImg;
				$rdArray['gwPrice'] 	= $objRd->gwPrice;
				$rdArray['tags'] 		= $objRd->tags;
				$rdArray['img'] 		= $objRd->img;
				$rdArray['is_wrapping'] = $objRd->is_wrapping;
			}
			
			/**
			 * Attribute array
			 */
			if (!isset($rdArray['attribute']))
			{
				$rdArray['attribute'] = array();
			}
			
			$rdArray['attribute'][] = array(
				'colour' => $objRd->id_color,
				'size' => $objRd->id_size,
				'stock' => $objRd->catalog_stock
			);
			
			/**
			 * Color array
			 */
			if ($objRd->catalog_stock > 0)
			{
				if (!isset($rdArray['colour'][$objRd->id_color][$objRd->id_size]))
				{
					$rdArray['colour'][$objRd->id_color][$objRd->id_size] = array();
				}
				
				$rdArray['colour'][$objRd->id_color][$objRd->id_size][] = $objRd->catalog_stock;
				
			}
		}
		
		$sessionid = session_id();
		
		if (!isset($_SESSION['RELATED_PRODUCTS']) || !is_array($_SESSION['RELATED_PRODUCTS']))
		{
			$_SESSION['RELATED_PRODUCTS'] = array();
			
			$_SESSION['RELATED_PRODUCTS'][$sessionid] = array();
			array_push($_SESSION['RELATED_PRODUCTS'][$sessionid], $rdArray);
		}			
		else
		{
			if (!isset($_SESSION['RELATED_PRODUCTS'][$sessionid]) || !is_array($_SESSION['RELATED_PRODUCTS'][$sessionid]))
			{
				$_SESSION['RELATED_PRODUCTS'][$sessionid] = array();
				array_push($_SESSION['RELATED_PRODUCTS'][$sessionid], $rdArray);
			}
			else
			{
				if (in_array($rdArray, $_SESSION['RELATED_PRODUCTS'][$sessionid]))
				{
					$key = array_search($rdArray, $_SESSION['RELATED_PRODUCTS'][$sessionid]);
					
					unset($_SESSION['RELATED_PRODUCTS'][$sessionid][$key]);
					
					array_push($_SESSION['RELATED_PRODUCTS'][$sessionid], $rdArray);
					$_SESSION['RELATED_PRODUCTS'][$sessionid] = array_values($_SESSION['RELATED_PRODUCTS'][$sessionid]);
				}
				else
				{
					if (count($_SESSION['RELATED_PRODUCTS'][$sessionid]) < 20)
						array_push($_SESSION['RELATED_PRODUCTS'][$sessionid], $rdArray);
					else
					{
						array_pop($_SESSION['RELATED_PRODUCTS'][$sessionid]);
						array_push($_SESSION['RELATED_PRODUCTS'][$sessionid], $rdArray);
					}
				}
			}
		}
		
		return $rdArray;
	}
}

?>